/**
 * 
 */
package net.iusr.jrules.eclipse;

import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.tptp.platform.analysis.codereview.java.CodeReviewResource;
import org.eclipse.tptp.platform.analysis.core.AnalysisUtil;
import org.eclipse.tptp.platform.analysis.core.category.IAnalysisCategory;
import org.eclipse.tptp.platform.analysis.core.history.AnalysisHistory;
import org.eclipse.tptp.platform.analysis.core.provider.AbstractAnalysisProvider;

/**
 * 
 * @author <a href="mailto:iusr.jar@gmail.com">IUSR</a>
 * @since 2006-11-13 下午08:56:56
 * @version $Revision$
 */
public class JRulesProvider extends AbstractAnalysisProvider {
	@Override
	public void analyze(IProgressMonitor monitor, AnalysisHistory history) {
		List<IResource> filteredResources = getJavaCodeResource();
		monitor.beginTask(getLabel(), filteredResources.size());

		// Iterate through each selected resource file
		for (IResource res : filteredResources) {
			if (monitor.isCanceled()) {
				return;
			}

			CodeReviewResource codeReviewRes = new CodeReviewResource(res);
			setProperty(history.getHistoryId(), RESOURCE_PROPERTY, codeReviewRes);

			List<IAnalysisCategory> cats = getAnalysisCategories();
			for (IAnalysisCategory category : cats) {
				if (monitor.isCanceled()) {
					return;
				}

				if (history.containsAnalysisElement(category)) {
					category.analyze(history);
				}
			}
			// Tell anyone who cares that we have finished examining 1 file
			getProviderManager().notifyAnalysisListeners(this);
			monitor.worked(1);
		}
		removeProperty(history.getHistoryId(), RESOURCE_PROPERTY);
		monitor.done();
	}

	@SuppressWarnings("unchecked")
	List<IResource> getJavaCodeResource() {
		return AnalysisUtil.getFilteredResources(getResources(), "java");
	}

	@SuppressWarnings("unchecked")
	List<IAnalysisCategory> getAnalysisCategories() {
		return super.getOwnedElements();
	}

	static final String RESOURCE_PROPERTY = "resource"; //$NON-NLS-1$
}
